/**   
 * @Title: CommonUtil.java 
 * @Package com.cdtv.util.file 
 * @Description: 
 * @author zhuw
 * @date 2012-10-26 下午4:36:17 
 */
package com.util;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.adpter.CusBaseAdapter;
import com.android.ocean.R;
import com.android.ocean.base.ExitApplication;
import com.android.ocean.util.common.StringUtil;
import com.modle.JobStruct;
import com.modle.User;

/**
 * @ClassName: AppUtil
 * @Description:
 * @author py
 * 
 */
public class AppUtil {
	


    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final Paint paint = new Paint();
            //保证是方形，并且从中心画
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int w;
            int deltaX = 0;
            int deltaY = 0;
            if (width <= height) {
                w = width;
                deltaY = height - w;
            } else {
                w = height;
                deltaX = width - w;
            }
            final Rect rect = new Rect(deltaX, deltaY, w, w);
            final RectF rectF = new RectF(rect);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            //圆形，所有只用一个
            
            int radius = (int) (Math.sqrt(w * w * 2.0d) / 2);
            canvas.drawRoundRect(rectF, radius, radius, paint);

            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }


	public static boolean isLegalVideo(String videoUrl) {
		boolean flag = false;
		if (videoUrl != null
				&& (videoUrl.endsWith(".mp4") || videoUrl.endsWith(".3gp"))) {
			flag = true;
		}
		return flag;
	}

	/***
	 * 
	 * @Description: 启动Activity
	 * @param ctx
	 * @param packageName
	 */
	public static void startApk(final Context ctx, String packageName) {
		PackageManager pm = ctx.getPackageManager();
		PackageInfo pi;
		try {
			pi = pm.getPackageInfo(packageName, 0);
			Intent intent = new Intent(Intent.ACTION_MAIN, null);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.setPackage(pi.packageName);

			List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

			ResolveInfo ri = apps.iterator().next();
			if (ri != null) {
				String className = ri.activityInfo.name;
				intent.setComponent(new ComponentName(packageName, className));
				ctx.startActivity(intent);
			}
		} catch (NameNotFoundException e) {
		}
	}

	public static void tsMsg(Context mContext, String msg) {
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();

	}

	public static void tlMsg(Context mContext, String msg) {
		Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();

	}

	/**
	 * 
	 * @Description:TODOTab爱艺人列表
	 * @param@param mContext
	 * @param@param conListView
	 * @param@param conList
	 * @param@param conListAdapter
	 * @param@param selectPos
	 * @return void
	 * @author zhuw
	 * @date 2013-7-24 下午2:36:11
	 */
	public static void fillXListViewIyrListData(final Context mContext,
			ListView conListView, final ArrayList<User> dataList,
			CusBaseAdapter conListAdapter, int selectPos) {
		if (StringUtil.isListNull(dataList)) {
			AppUtil.tsMsg(mContext, "无艺人数据...");
		} else {

			if (conListAdapter == null) {
			//	conListAdapter = new IyrUserListAdapter(dataList, mContext);
			} else {
				conListAdapter.notifyDataSetChanged();
			}
			conListView.setAdapter(conListAdapter);
			conListView.setSelection(selectPos);
			conListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int pos, long arg3) {
					if (pos > 0 && pos <= dataList.size()) {
						Bundle b = new Bundle();
						b.putSerializable("user", dataList.get(pos - 1));
						//TranHelper.toAct(mContext, YrInfoActivity.class, b);

					}
				}
			});

		}

	}


    /**
     * 
     * @Description:TODO跳转到查看职位界面
     * @param@param mContext
     * @param@param job
     * @return void
     * @author zhuw
     * @date 2013-8-23 下午10:21:33
     */
	public static void jobSwitch(Context mContext, JobStruct job) {
		Bundle b = new Bundle();
		b.putSerializable("job", job);
		//TranHelper.toAct(mContext, ViewJobActivity.class, b);
	}
	/**
	 * 
	 * @Description:TODO跳转打礼物列表
	 * @param@param mContext
	 * @param@param userID
	 * @param@param type
	 * @param@param title
	 * @return void
	 * @author zhuw
	 * @date 2013-8-24 下午12:51:21
	 */
	public static void giftListSwitch(Context mContext, String userID,String type,String title) {
		Bundle b = new Bundle();
		b.putString("userID",userID);
		b.putString("title", title);
		b.putString("type", type);
		//TranHelper.toAct(mContext, GiftListActivity.class, b);
	}
	
	/**
	 * 
	 * @Description: 退出程序
	 * @param mContext
	 */
	public static void exitApk(Context mContext) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setIcon(R.drawable.info_ico).setTitle("提示");
		builder.setMessage("是否退出程序?");

		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ExitApplication.getInstance().exit();
			}
		});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.show();

	}
	public static void openWebOutter(Context ctx, String url) {
		Intent mIntent = new Intent();
		mIntent.setAction("android.intent.action.VIEW");
		mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri content_url = Uri.parse(url);
		mIntent.setData(content_url);
		ctx.startActivity(mIntent);

	}
}
